package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoRedisService redisService;
    @Autowired
    private ITravelCommentService commentService;

    @GetMapping("query")
    public Object list(Model model, @ModelAttribute("qo") TravelQuery qo){

        return JsonResult.success(travelService.query(qo));
    }
    @GetMapping("detail")
    public Object travelDetail(String id){

        return JsonResult.success(travelService.get(id));
    }

    @RequireLogin
    @RequestMapping("input")
    public Object input(@UserParam UserInfo userInfo){

        return JsonResult.success();
    }

    //上传图片
    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @RequireLogin
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = redisService.getUserByToken(token);
        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    /**
     * 使用简化的方式获取当前登录用户信息
     *直接在请求映射方法中声明Userinfo这个类型形式参数
     * springmvc就自动将当前登录用户注入
     */
    @GetMapping("info")
    public Object info(@UserParam UserInfo userInfo){
        //获取当前登录用户信息
        return JsonResult.success(userInfo);
    }

    //定义一个参数级别注解,当参数贴有这个注解,自定义解析器才执行解析逻辑
    @GetMapping("updateInfo")
    public Object updateInfo(@UserParam UserInfo userInfo){
        //获取当前登录用户信息
        return JsonResult.success(userInfo);
    }

    @RequireLogin
    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment comment,@UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        //保存评论
        commentService.save(comment);
        return JsonResult.success();
    }
    @GetMapping("comments")
    public Object comments(String travelId){

        return JsonResult.success(commentService.queryCommentByTravelId(travelId));
    }

    @GetMapping("newComment")
    public Object queryNewTravel(){

        return JsonResult.success(commentService.queryNewTravelComment());
    }

}
